#include "KeyManager.h"
#include "Globals.h"

KeyManager * KeyManager::instance = NULL;

KeyManager* KeyManager::get()
{
	if (!instance) 
		instance = new KeyManager();
	return instance;
}

void KeyManager::injectKeyDown(SDLKey key)
{
	switch(key)
	{
	case SDLK_ESCAPE:
		Globals::get()->setIsRunning(false); break;
	case SDLK_UP:
		mKeys[GKEY_UP] = true; break;
	case SDLK_DOWN:
		mKeys[GKEY_DOWN] = true; break;
	case SDLK_LEFT:
		mKeys[GKEY_LEFT] = true; break;
	case SDLK_RIGHT:
		mKeys[GKEY_RIGHT] = true; break;
	case SDLK_w:
		mKeys[GKEY_W] = true; break;
	case SDLK_s:
		mKeys[GKEY_S] = true; break;
	case SDLK_a:
		mKeys[GKEY_A] = true; break;
	case SDLK_d:
		mKeys[GKEY_D] = true; break;
	case SDLK_p:
		mKeys[GKEY_P] = true; break;
	case SDLK_o:
		mKeys[GKEY_O] = true; break;
	case SDLK_b:
		mKeys[GKEY_B] = true; break;
	case SDLK_n:
		mKeys[GKEY_N] = true; break;
	case SDLK_m:
		mKeys[GKEY_M] = true; break;
	case SDLK_v:
		mKeys[GKEY_V] = true; break;
	default:
		mKeys[GKEY_UNSUPPORTED] = true; break;
	}
}

void KeyManager::injectKeyUp(SDLKey key)
{
	switch(key)
	{
	case SDLK_UP:
		mKeys[GKEY_UP] = false; break;
	case SDLK_DOWN:
		mKeys[GKEY_DOWN] = false; break;
	case SDLK_LEFT:
		mKeys[GKEY_LEFT] = false; break;
	case SDLK_RIGHT:
		mKeys[GKEY_RIGHT] = false; break;
	case SDLK_w:
		mKeys[GKEY_W] = false; break;
	case SDLK_s:
		mKeys[GKEY_S] = false; break;
	case SDLK_a:
		mKeys[GKEY_A] = false; break;
	case SDLK_d:
		mKeys[GKEY_D] = false; break;
	case SDLK_p:
		mKeys[GKEY_P] = false; break;
	case SDLK_o:
		mKeys[GKEY_O] = false; break;
	case SDLK_b:
		mKeys[GKEY_B] = false; break;
	case SDLK_n:
		mKeys[GKEY_N] = false; break;
	case SDLK_m:
		mKeys[GKEY_M] = false; break;
	case SDLK_v:
		mKeys[GKEY_V] = false; break;
	default:
		mKeys[GKEY_UNSUPPORTED] = false; break;
	}
}

bool KeyManager::isKeyDown(KeyCode key)
{
	return (mKeys[key] == true);
}

bool KeyManager::isKeyUp(KeyCode key)
{
	return (mKeys[key] == false);
}

KeyManager::KeyManager()
{
	for (int i=0; i < GKEY_COUNT; ++i)
		mKeys[i] = false;
}

KeyManager::~KeyManager()
{
}
